package com.ruoyi.business.service.impl;

import com.ruoyi.business.service.ProcessService;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

@Service
public class ProcessServiceImpl implements ProcessService {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    @Override
    public Deployment deploy(String filePath) throws FileNotFoundException {
        Deployment deployment = null;
        if (filePath.endsWith("zip")) {
            ZipInputStream inputStream = new ZipInputStream(new FileInputStream(filePath));
            deployment = repositoryService.createDeployment().addZipInputStream(inputStream).deploy();
        } else {
            deployment = repositoryService.createDeployment().addInputStream(filePath, new FileInputStream(filePath)).deploy();
        }
        return deployment;
    }

    @Override
    public InputStream getBpmnInputStream(String deployId) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployId).singleResult();
        System.out.println(processDefinition.getResourceName());
        return repositoryService.getResourceAsStream(deployId, processDefinition.getResourceName());
    }

    @Override
    public InputStream getProcessImg(String deployId) {
        //获取流程定义id
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployId).singleResult();
        //获取bpmnModel,主要描述节点,连线的信息
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
        //定义绘制图形对象
        DefaultProcessDiagramGenerator processDiagramGenerator = new DefaultProcessDiagramGenerator();
        //参数一:bpmnModel流程图 参数二:需要高亮的节点集合 参数二:需要高亮的连线集合
        InputStream inputStream = processDiagramGenerator.generateDiagram(bpmnModel, Collections.EMPTY_LIST, Collections.EMPTY_LIST, "宋体", "宋体", "宋体");
        return inputStream;
    }

    @Override
    public ProcessInstance startProcessInstance(String definitionKey, String businessKey, Map<String, Object> variables) {
        return runtimeService.startProcessInstanceByKey(definitionKey, businessKey, variables);
    }

    @Override
    public Task getTaskByProcessInstanceId(String processInstanceId) {
        return taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
    }

    @Override
    public void addCandidateUser(Task currentTask, Long userId) {
        taskService.addCandidateUser(currentTask.getId(), String.valueOf(userId));
    }

    @Override
    public Task selectTaskByInstanceId(String instanceId) {
        return taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
    }

    @Override
    public List<String> getActiveActivityIds(String instanceId) {
        return runtimeService.getActiveActivityIds(instanceId);
    }

    @Override
    public InputStream getProcessImage(String processDefinitionId, List<String> highLightedActivities, List<String> highLightedFlows) {
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
        DefaultProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
        InputStream inputStream = generator.generateDiagram(bpmnModel, highLightedActivities, highLightedFlows, "宋体", "宋体", "宋体");
        return inputStream;
    }

    @Override
    public ProcessInstance getProcessInstanceByInstanceId(String instanceId) {
        return runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).singleResult();
    }

    @Override
    public void deleteProcessInstance(String instanceId, String tips) {
        runtimeService.deleteProcessInstance(instanceId, tips);
    }

    @Override
    public Long selectTodoTaskListCount(String definitionKey, String userId) {
        return taskService.createTaskQuery().processDefinitionKey(definitionKey).taskCandidateUser(userId).count();
    }

    @Override
    public List<Task> selectTodoTaskList(String definitionKey, String userId, Integer startNum, Integer pageSize) {
        return taskService.createTaskQuery().processDefinitionKey(definitionKey).taskCandidateUser(userId).orderByTaskCreateTime().desc().listPage(startNum, pageSize);
    }

    @Override
    public Task getTaskByTaskId(String taskId) {
        return taskService.createTaskQuery().taskId(taskId).singleResult();
    }

    @Override
    public void claimAndComplateTask(Task task, String userId, Boolean auditStatus, String commentStr) {
        //添加批注
        taskService.addComment(task.getId(),task.getProcessInstanceId(),commentStr);
        //设置流程变量
        taskService.setVariable(task.getId(),task.getTaskDefinitionKey(),auditStatus);
        //认领任务
        taskService.claim(task.getId(),userId);
        //完成
        taskService.complete(task.getId());
    }
}
